module sw.mode;
/**
 * モードを扱うモジュール
 */
import nazo.http, nazo.array, sw.siteconfig, nazo.stdio, std.stdio, sw.client;

///モードのインターフェース
interface IMode{
  ///レスポンスを書き出す。
  void responds();
}

enum ModeMethod{
  GET, POST
}

///モード
/*abstract*/ class Mode(ModeMethod m = ModeMethod.GET):IMode{
  SiteConfig* cfg;
  this(SiteConfig* cfg){
    this.cfg = cfg;
  }
  ///パーミッションを返す。
  protected int permission(){
    static if(m == ModeMethod.POST)
      return Client.Authority.Guest | Client.Authority.Admin;
    else
      return Client.Authority.Guest | Client.Authority.Bot |
              Client.Authority.Admin;
  }
  protected abstract void response();
  ///レスポンスを書き出す。
  final void responds(){
    if(!(permission()&Client.authority))
      throw new SecurityException;

    static if(m == ModeMethod.POST){
      if(Http.isGetMethod)
        throw new SecurityException;
      if(!Http.isValidReferer)
        throw new SecurityException;
    }
    response();
  }
}

class SecurityException:Exception{
  this(){
    super("SecurityException: ");
  }
}

class InvalidArgsException:Exception{
  this(){
    super("InvalidArgsException: ");
  }
  this(string msg){
    super("InvalidArgsException: "~msg);
  }
}
